<?php
Database($_SESSION['cid']);
class QuestionnaireAction extends Action{

    private $questionnaire_state = [0=>'未开始',1=>'进行中',2=>'已结束'];

    private $bg                  = [0=>'panel-body bg-primary',1=>'panel-body bg-info',2=>'panel-body bg-success'];

//--------------------------------------insert-------------------------------------------------------------
    public function addAct()
    {
        $rs = D('Questionnaire')->add_questionnaire($_POST);
        if($rs)
            $this->success('添加成功',__URL__);
        else
            $this->error('添加失败');
    }

    public function add_question_act()
    {
        $rs = D('Questionnaire')->add_question($_POST);
        if($rs)
            $this->success('添加成功');
        else
            $this->error('添加失败');
    }
//--------------------------------------delete-------------------------------------------------------------
    public function del_questionaire($id)
    {
        $rs = M('Questionnaire','','DB_MEETING')->where(array('questionnaire_id' =>$_GET['questionnaire_id'])) ->save($data);
    }

    public function delAct()
    {
        // 李记辉 更新于  2015/01/16

        $rs = M('questionnaire','','DB_MEETING')->delete($_GET['questionnaire_id']);
        if($rs)
            $this->success('删除成功',__URL__);
        else
            $this->error('删除失败');
    }

    public function del_question(){
        // 李记辉  添加于 2015/01/29
        $rs = M('questionnaire_question','','DB_MEETING')->delete($_GET['question_id']);
        if($rs)
            $this->success('删除成功',__URL__);
        else
            $this->error('删除失败');
    }
    
    public function del_question_act()
    {   
        // 李记辉  更改于 2015/01/16
        $id = M('questionnaire_question' , '' , 'DB_MEETING')->where('question_id='.$_POST['question_id'])->find();

        $rs = M('questionnaire_question','','DB_MEETING')->delete($_POST['question_id']);
        if($rs)
            $this->success('删除成功',__URL__.'/to_add_question?questionnaire_id='.$id['questionnaire_id']);
        else
            $this->error('删除失败');
    }
//--------------------------------------update----------------------------------------------------------------
    public function to_update()
    {
        // 李记辉 更新于 2015/01/16
        $rs = M('questionnaire' , '' , 'DB_MEETING')->where('questionnaire_id='.$_GET['questionnaire_id'])->find();
        $rs['time'] = date('Y-m-d' , $rs['questionnaire_end_time']);
        
        $this ->assign('rs' , $rs);
        $this ->display();       
    }

    public function upt_question_act()
    {
        // 李记辉 更新于 2015/01/16
        $_POST['questionnaire_end_time'] = strtotime($_POST['time']);
        unset($_POST['time']);
        $rs = M('questionnaire' , '' ,'DB_MEETING')->where('questionnaire_id='.$_POST['questionnaire_id'])->save($_POST);

        if($rs)
            $this->success('修改成功',__URL__.'/index');
        else
            $this->error('修改失败');
    }

    public function update_state()
    {
        $rs = D('Questionnaire')->update_state($_GET);
        if($rs)
            echo json_encode(array("state"=>0));
        else
            echo json_encode(array("state"=>-1));
    }
//--------------------------------------select-------------------------------------------------------------
    public function index()
    {
        $rs = pageing('questionnaire',10,'',$_GET['seach'],'questionnaire_state','1','questionnaire_title','','DB_MEETING');
        foreach($rs['data'] as &$val)
        {
            $val['questionnaire_question_num'] = D('Questionnaire')->get_question_num($val['questionnaire_id']);
            $val['questionnaire_audience_num'] = D('Questionnaire')->get_audience_num($val['questionnaire_id']);
            $val['questionnaire_state_1']      = $val['questionnaire_state']+1 == 3 ? 0 : $val['questionnaire_state']+1;
            $val['questionnaire_state_1_1']    = $this->questionnaire_state[$val['questionnaire_state_1']];
            $val['bg_color']                   = $this->bg[$val['questionnaire_state']];
            $val['questionnaire_state']        = $this->questionnaire_state[$val['questionnaire_state']];
        }
        $this->assign("rs",$rs);
        $this->display();
    }

    public function to_add()
    {
        $this->display();
    }

    public function to_upt()
    {
        $rs = D('Questionnaire')->get_questionnaire($_GET['questionnaire_id']);
        $this->assign("rs",$rs);
        $this->display();
    }

    public function to_add_question()
    {
        $rs = D('Questionnaire')->get_questions($_GET['questionnaire_id']);
        $re = D('Questionnaire')->get_audience_answer_num($rs);
        $rz = D('Questionnaire')->get_detail($re);
        $r = M('questionnaire' , '' , 'DB_MEETING')->where('questionnaire_id='.$_GET['questionnaire_id'])->find();
        $this->assign("re",$re);
        $this ->assign("rz" , $rz);
        $this ->assign("r" , $r);
        $this->display();
    }
}
